package com.hesicare.cgm.service.impl;

import com.hesicare.cgm.entity.CgmBloodGlucoseDTO;
import com.hesicare.cgm.entity.CgmPatientGlucose;
import com.hesicare.health.mapper.cgm.CgmBloodGlucoseDayMapper;
import com.hesicare.cgm.service.ICgmBloodGlucoseDayService;
import com.hesicare.health.entity.SysPatient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 55097
 * @create 2021/12/6 14:43
 */
@Service
@Primary
public class CgmBloodGlucoseDayServiceImpl implements ICgmBloodGlucoseDayService {

    @Autowired
    private CgmBloodGlucoseDayMapper cgmBloodGlucoseDayMapper;


    protected final static Logger log = LoggerFactory.getLogger(CgmBloodGlucoseDayServiceImpl.class);


    /**
     * 批量新增cgm血糖记录
     *
     * @param cgmPatientGlucose cgm血糖记录请求对象
     * @param sysPatient        cgm患者请求对象
     * @return 插入记录数
     */
    @Override
    public void insertCgmBloodGlucoseDay(CgmPatientGlucose cgmPatientGlucose, SysPatient sysPatient) {
        List<CgmBloodGlucoseDTO> cgmBloodGlucoseList = new ArrayList<>();
        cgmPatientGlucose.getGlucoseData().stream().forEach(cgmBlucoseData -> {
            CgmBloodGlucoseDTO cgmBloodGlucoseDTO = new CgmBloodGlucoseDTO();
            cgmBloodGlucoseDTO.setPatientId(sysPatient.getPatientId());
            cgmBloodGlucoseDTO.setDeptId(sysPatient.getDeptId());
            cgmBloodGlucoseDTO.setMeasureTime(new Date(cgmBlucoseData.getProcessedTime()));
            cgmBloodGlucoseDTO.setMeasureDate(new Date(cgmBlucoseData.getProcessedTime()));
            cgmBloodGlucoseDTO.setMeasureValue(cgmBlucoseData.getGlucoseValue());
            cgmBloodGlucoseDTO.setStatus("0");
            cgmBloodGlucoseDTO.setCreateTime(new Date());
            cgmBloodGlucoseDTO.setStudyId(String.valueOf(cgmBlucoseData.getIndex()));
            cgmBloodGlucoseList.add(cgmBloodGlucoseDTO);
        });
        if (!cgmBloodGlucoseList.isEmpty()) {
            cgmBloodGlucoseDayMapper.insertBatchCgmBloodGlucoseDay(cgmBloodGlucoseList);
        }
    }
}
